home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 July: Mac OS SDK / Dev.CD Jul 97 SDK1.toast / Development Kits (Disc 1) / Apple Shared Library Manager / ASLM Examples / Sample Apps / CPlusSample / Sources / SampleLibrary.cp < prev    next >
Encoding:
Text File  |  1996-11-19  |  8.0 KB  |  266 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        SampleLibrary.cp
  3.  
  4.     Contains:    Implementation of the CPlusSampleLibrary shared library.
  5.  
  6.     Copyright:    © 1993-1994 by Apple Computer, Inc., all rights reserved.
  7.  
  8. */
  9.  
  10.  
  11. #include <GlobalNew.h>
  12. #include <LibraryManager.h>
  13. #include <LibraryManagerClasses.h>
  14. #include <LibraryManagerUtilities.h>
  15.  
  16. #include <String.h>
  17. #include <Events.h>
  18. #include <QuickDraw.h>
  19. #include <Resources.h>
  20. #include <Menus.h>
  21. #include <Windows.h>
  22. #include <StdIO.h>
  23.  
  24. #include "SampleLibrary.h"
  25.  
  26. //————————————————————————————————————————————————————————————————————————————————————
  27. //    TTrafficLight
  28. //    
  29. //    TrafficLight constructor, allocates window record from client's pool, and ini-
  30. //    tialize our light rectangles.
  31. //————————————————————————————————————————————————————————————————————————————————————
  32.  
  33. TTrafficLight::TTrafficLight()
  34. {
  35.     long     savedrefnum = -1;
  36.     Ptr     wstorage = nil;
  37.     
  38.     VOLATILE(wstorage);                // use this if variable is being modified within
  39.     VOLATILE(savedrefnum);            // TRY and accessed in the CATCH_ALL.
  40.  
  41.     fWindow = nil;
  42.     
  43.     Trace("TTrafficLight Constructor\n");// send the output to trace monitor's window
  44.  
  45.     TRY
  46.         fLightState = false;
  47.         
  48.         wstorage = (Ptr)new WindowRecord;// allocate our storage
  49.     
  50.         FailNULL( wstorage, ErrorCode(), "sorry not enought memory in pool");
  51.                   
  52.         // include the library's file in resource chain to obtain our menus & windows
  53.  
  54.         Fail(GetLocalLibraryFile()->Preflight(savedrefnum), "Failed to preflight");
  55.         
  56.         fWindow = GetNewWindow( rWindow, wstorage, (WindowPtr) -1 );
  57.             
  58.         FailNULL( fWindow, ResError(), "sorry unable to create a window");
  59.     
  60.         TTrafficLight::GoGetRect(rStopRect, &fStopRect); // get rectangle for Stop light
  61.         TTrafficLight::GoGetRect(rGoRect, &fGoRect); // get rectangle for Go light
  62.     
  63.         // Any MENU resources in our shared library is appended to our application
  64.     
  65.         MenuHandle themenu = GetMenu( mLight );    // get our traffic light menu
  66.         InsertMenu( themenu, 0 );        // append to the application's menu
  67.         
  68.         DrawMenuBar();                    // go ahead and update the menu bar
  69.     CATCH_ALL                            // oh no! something failed lets cleanup
  70.         delete wstorage;                // remove the storage
  71.         delete fWindow;                    // free the pool
  72.     ENDTRY
  73.  
  74.     if (savedrefnum != -1)
  75.         GetLocalLibraryFile()->Postflight(savedrefnum); // restore the resource chain
  76. }
  77.  
  78. //————————————————————————————————————————————————————————————————————————————————————
  79. //    ~TTrafficLight
  80. //    
  81. //    our traffic light destructor, delete the window, and unregister the object for
  82. //    inspector
  83. //————————————————————————————————————————————————————————————————————————————————————
  84.  
  85. TTrafficLight::~TTrafficLight()
  86. {
  87.     Trace("TTrafficLight Destructor\n");// tell Trace Monitor where we are
  88.  
  89.     CloseWindow( fWindow );                // close the window
  90.     delete fWindow;                        // we are done, release the memory
  91. }
  92.  
  93. //————————————————————————————————————————————————————————————————————————————————————
  94. //    IsValid
  95. //    
  96. //    returns true if the object was initialized properly after it was created. In our
  97. //  case if window has been allocated, we return true.
  98. //————————————————————————————————————————————————————————————————————————————————————
  99.  
  100. Boolean TTrafficLight::IsValid() const
  101. {
  102.     return( fWindow != nil );            // return true if window has been allocated
  103. }
  104.  
  105. //————————————————————————————————————————————————————————————————————————————————————
  106. //    GetLightState
  107. //    
  108. //    return current state of traffic light
  109. //————————————————————————————————————————————————————————————————————————————————————
  110.  
  111. Boolean TTrafficLight::GetLightState() const
  112. {
  113.     Trace("TTrafficLight::GetLightState\n");    // tell Trace Monitor where we are 
  114.     return fLightState;                    // return the state of Traffic Light
  115. }
  116.  
  117. //————————————————————————————————————————————————————————————————————————————————————
  118. //    SetLightState
  119. //    
  120. //    set the light to the new state
  121. //————————————————————————————————————————————————————————————————————————————————————
  122.  
  123. void TTrafficLight::SetLightState( Boolean newState )
  124. {
  125.     Trace("TTrafficLight::SetLightState\n");    // tell Trace Monitor what we are doing
  126.     fLightState = newState;                // set the new state
  127.     
  128.     SetPort(fWindow);                    // make sure we are at the right port before
  129.     InvalRect(&fWindow->portRect);        // invalidating the update region
  130. }
  131.  
  132. //————————————————————————————————————————————————————————————————————————————————————
  133. //    DrawLight
  134. //    
  135. //    draw the actual traffic light
  136. //————————————————————————————————————————————————————————————————————————————————————
  137.  
  138. void TTrafficLight::DrawLight()
  139. {
  140.     GrafPtr        oldport;
  141.     
  142.     Trace("TTrafficLight::DrawLight\n");    // tell the Trace Monitor what we are doing
  143.  
  144.     if( fWindow ) {
  145.     
  146.         GetPort( &oldport );            // save the old port
  147.         SetPort( fWindow );                // set the new port to our object's port
  148.                 
  149.         EraseRect(&fWindow->portRect);    // clear out any garbage that may linger
  150.         if ( fLightState )                // draw a red (or white) stop light
  151.             ForeColor(redColor);
  152.         else
  153.             ForeColor(whiteColor);
  154.         
  155.         PenSize( 2, 2 );                
  156.         PaintOval(&fStopRect);            
  157.         ForeColor(blackColor);
  158.         FrameOval(&fStopRect);
  159.         if ( ! fLightState )            // draw a green (or white) go light
  160.             ForeColor(greenColor);
  161.         else
  162.             ForeColor(whiteColor);
  163.         PaintOval(&fGoRect);
  164.         ForeColor(blackColor);
  165.         FrameOval(&fGoRect);
  166.         PenSize( 1, 1 );
  167.  
  168.         SetPort( oldport );                // restore the grafpointer
  169.     }
  170. }
  171.  
  172. //————————————————————————————————————————————————————————————————————————————————————
  173. //    AdjustMenus
  174. //    
  175. //    Enable and disable traffic light's menus based on the current state.
  176. //————————————————————————————————————————————————————————————————————————————————————
  177.  
  178. void TTrafficLight::AdjustMenus( Boolean active )
  179. {
  180.     long                savedrefnum;
  181.     MenuHandle            menu;
  182.     OSErr                err;
  183.  
  184.     Trace("TTrafficLight::AdjustMenus\n");// tell the Trace Monitor where we are
  185.  
  186.     // include the library in resource chain so we can allocate our menu resources
  187.     
  188.     if( err = GetLocalLibraryFile()->Preflight( savedrefnum ) ) {
  189.         Trace("Preflight failed err=%d\n", err);// let Trace Monitor know what failed
  190.         return;
  191.     }
  192.         
  193.     if(  menu = GetMenuHandle(mLight) ) {    // get the handle to traffic light menu
  194.         
  195.         if ( active ) {                    // do we need to enable them?
  196.             EnableItem(menu, iStop);
  197.             EnableItem(menu, iGo);
  198.         } else {
  199.             DisableItem(menu, iStop);
  200.             DisableItem(menu, iGo);
  201.         }
  202.  
  203.         CheckItem(menu, iStop, fLightState); // we can also determine check/uncheck state, too
  204.         CheckItem(menu, iGo, ! fLightState);        
  205.     }
  206.     else
  207.         Trace("GetMenuHandle returned NIL\n");    // let Trace Monitor know we failed
  208.  
  209.     // restore the resource chain to it previous state
  210.     
  211.     err = GetLocalLibraryFile()->Postflight( savedrefnum );
  212. }
  213.  
  214. //————————————————————————————————————————————————————————————————————————————————————
  215. //    DoMenuCommand
  216. //    
  217. //    Enable and disable menus based on the current state of traffic light.
  218. //————————————————————————————————————————————————————————————————————————————————————
  219.  
  220. void TTrafficLight::DoMenuCommand( short menuItem )
  221. {
  222.     long            savedrefnum;
  223.     OSErr            err;
  224.  
  225.     Trace("TTrafficLight::DoMenuCommand\n");    // tell Trace Monitor what we are doing
  226.                                             // include the library's file in resource chain
  227.     if( err = GetLocalLibraryFile()->Preflight( savedrefnum ) ) {
  228.         Trace("Preflight failed err=%d\n", err);// let Trace Monitor know what failed
  229.         return;
  230.     }
  231.  
  232.     switch ( menuItem ) {                    // what menu item selected?
  233.         case iStop:
  234.             SetLightState( true );                // set and update
  235.             break;
  236.         case iGo:
  237.             SetLightState( false );                // set and  update
  238.             break;
  239.     }
  240.                                             // restore the resource change 
  241.     err = GetLocalLibraryFile()->Postflight(savedrefnum);
  242. }
  243.  
  244. //————————————————————————————————————————————————————————————————————————————————————
  245. //    GoGetRect
  246. //    
  247. //    loads the global rectrangles that are used for drawing the traffic lights.
  248. //————————————————————————————————————————————————————————————————————————————————————
  249.     
  250. Boolean TTrafficLight::GoGetRect( short rectID, Rect *theRect)
  251. {
  252.     Handle        resource;
  253.     
  254.     TRY
  255.         resource = GetResource('RECT', rectID);    // get 'RECT' resource from library file
  256.         FailNULL( resource, ResError(), "'RECT' resource is missing" );
  257.         *theRect = **((Rect**) resource);
  258.     CATCH_ALL
  259.         DebugStr("\pFailed to get 'RECT' resource");// inform the user we failed
  260.         SetRect( theRect, 10, 10, 60, 60 );
  261.         return false;
  262.     ENDTRY
  263.     
  264.     return true;
  265. }
  266.